package com.hcms.admin.controller.sys;

import com.hcms.admin.annotation.CheckPermission;
import com.hcms.admin.dto.UcenterChangePasswordRequest;
import com.hcms.admin.security.AdminUserDetails;
import com.hcms.admin.service.ISysAdminService;
import com.hcms.common.api.CommonResponse;
import com.hcms.mybatis.entity.SysAdmin;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/ucenter")
@Api("个人中心")
public class UcenterController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ISysAdminService sysAdminService;

    @GetMapping("")
    @ResponseBody
    @CheckPermission(check = false)
    @ApiOperation("获取个人中心数据")
    public ResponseEntity<CommonResponse<SysAdmin>> index() {
        logger.info("Run index()");
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        AdminUserDetails principal = (AdminUserDetails) authentication.getPrincipal();
        SysAdmin sysAdmin = principal.getSysAdmin();
        return CommonResponse.ok(sysAdmin);
    }

    @PostMapping("")
    @ResponseBody
    @CheckPermission(check = false)
    @ApiOperation("设置个人中心数据")
    public ResponseEntity<CommonResponse<Object>> index(@RequestBody SysAdmin entity) {
        logger.info("Run index(), username={}", entity.getUsername());
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        AdminUserDetails principal = (AdminUserDetails) authentication.getPrincipal();
        SysAdmin sysAdmin = principal.getSysAdmin();
        //保存用户基本数据
        SysAdmin save = new SysAdmin();
        save.setId(sysAdmin.getId());
        save.setNickName(entity.getNickName());
        save.setIcon(entity.getIcon());
        save.setEmail(entity.getEmail());
        save.setNote(entity.getNote());
        int count = sysAdminService.updateItem(sysAdmin.getId(), save);
        if (count > 0) {
            logger.info("Save successfully");
            return CommonResponse.accepted("修改成功");
        }
        logger.error("Fail to change");
        return CommonResponse.badRequest("修改失败");
    }

    @PostMapping("/changePassword")
    @ResponseBody
    @CheckPermission(check = false)
    @ApiOperation("用户修改密码")
    public ResponseEntity<CommonResponse<Object>> changePassword(@RequestBody UcenterChangePasswordRequest entity) {
        logger.info("Run changePassword()");
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        AdminUserDetails principal = (AdminUserDetails) authentication.getPrincipal();
        SysAdmin sysAdmin = principal.getSysAdmin();
        String password = entity.getPassword();
        String newPassword = entity.getNewPassword();
        //修改用户密码
        boolean success = sysAdminService.changePassword(sysAdmin.getId(), password, newPassword);
        if (success) {
            logger.info("Save successfully");
            return CommonResponse.accepted("修改成功");
        }
        logger.error("Fail to change");
        return CommonResponse.badRequest("修改失败");
    }

}
